package com.supor.aiot.common.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

/**
 * @author Xiaozhuang
 * @date 2021/11/1
 * @description 带滑动监听的WebView
 */
public class ScrollWebView extends WebView {

    private OnScrollChangedListener mOnScrollChangedListener;

    public ScrollWebView(Context context) {
        super(context);
    }

    public ScrollWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnScrollChangedListener(OnScrollChangedListener mOnScrollChangedListener) {
        this.mOnScrollChangedListener = mOnScrollChangedListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        float webContent = getContentHeight() * getScale();
        float webNow = getHeight() + getScrollY();
        if (Math.abs(webContent - webNow) < 1) {
            mOnScrollChangedListener.onPageEnd(l, t, oldl, oldt);
        } else if (getScrollY() == 0) {
            mOnScrollChangedListener.onPageTop(l, t, oldl, oldt);
        } else {
            mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
        }
    }

    public interface OnScrollChangedListener {
        void onPageTop(int l, int t, int oldl, int oldt);
        void onPageEnd(int l, int t, int oldl, int oldt);
        void onScrollChanged(int l, int t, int oldl, int oldt);
    }
}
